﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UserDefineForm.control;
using UserDefineForm.control.propertymapping;

namespace UserDefineForm.formcomponent
{
    public partial class PropertiesPanelv2 : UserControl
    {
        private DesignableControlBase control;
        private DesignPanel designPnl;
        private MappingObjectBase mappingObject;

        public PropertiesPanelv2(DesignPanel desPnl)
        {
            InitializeComponent();
            this.designPnl = desPnl;
        }

        public void setControl(DesignableControlBase control)
        {
            if (control == null)
            {
                propertyGrid.SelectedObject = null;
                return;
            }
            this.control = control;
            mappingObject = control.MappingObject ;
            propertyGrid.SelectedObject= mappingObject;
        }

        public void refreshInfo()
        {
            propertyGrid.Refresh();
        }

        private void deleteBtn_Click(object sender, EventArgs e)
        {
            designPnl.deleteControl(control);
            setControl(null);
        }

        private void applyChanged(object s, PropertyValueChangedEventArgs e)
        {
            if (!mappingObject.checkValidProperties())
            {
                e.ChangedItem.PropertyDescriptor.SetValue(propertyGrid.SelectedObject, e.OldValue);
                return;
            }

            mappingObject.applyChanges();

            propertyGrid.Refresh();
            
        }

        
    }


}
